﻿using DayuCode.Designer.Common;
using DayuCode.Designer.Entity;
using DayuCode.Designer.Service;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DayuCode.Designer.Forms
{
    public partial class FormProjectUser : UIForm
    {
        ProjectUser projectUser;
        public FormProjectUser(ProjectUser projectUser)
        {
            InitializeComponent();
            if (projectUser == null) throw new HException("projectUser不能为null");
            this.projectUser = projectUser;
            txtLoginId.Text = projectUser.LoginId;
            if (!string.IsNullOrEmpty(projectUser.LoginId))
            {
                txtLoginId.ReadOnly = true;
            }
            chkLoginAble.Checked = projectUser.IsLoginAble;
            chkIsAdmin.Checked = projectUser.IsAdmin;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string loginId = txtLoginId.Text.Trim();

            if (string.IsNullOrEmpty(loginId))
            {
                ShowErrorTip("loginId不能为空");
                return;
            }

            projectUser.LoginId = loginId;
            projectUser.setRoleCode(chkLoginAble.Checked, chkIsAdmin.Checked);
            if(projectUser.Id == null || projectUser.Id == 0)
            {
                projectUser = ProjectUserHandler.addProjectUser(projectUser.LoginId, (long)projectUser.ProjectId, projectUser.IsAdmin);
            }
            else
            {
                projectUser = ProjectUserHandler.changeProjectAdmin((long)projectUser.Id, projectUser.IsAdmin);
            }
            DialogResult = DialogResult.OK;
        }

        public ProjectUser ProjectUser
        {
            get { return projectUser; }
        }

        private void chkLoginAble_CheckedChanged(object sender, EventArgs e)
        {
            if (!chkLoginAble.Checked) chkIsAdmin.Checked = false;
        }

        private void chkIsAdmin_CheckedChanged(object sender, EventArgs e)
        {
            if (chkIsAdmin.Checked) chkLoginAble.Checked = true;
        }
    }
}
